package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.annotation.SuppressLint
import android.graphics.Paint
import android.os.Bundle
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.util.ImageLoaderManager
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityReserveDetailBinding
import com.gexiaobao.pigeon.ui.adapter.ProofPictureAdapter
import com.gexiaobao.pigeon.viewmodel.OrgDetailViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/5/24 9:09
 * @Describe    : 会员预定详情
 */
class ActivityReserveDetail : BaseActivity<OrgDetailViewModel, ActivityReserveDetailBinding>() {

    private var raceName: String = ""
    private var memberName: String = ""
    private var mRaceId = 0

    private val mAdapter: ProofPictureAdapter by lazy { ProofPictureAdapter(arrayListOf()) }
    private var dataImageList: MutableList<String> = arrayListOf()

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeOrgTitle.tvAllAppointmentInfo.paint.flags = Paint.UNDERLINE_TEXT_FLAG

        mRaceId = intent.getIntExtra("raceId", 0)
        memberName = intent.getStringExtra("memberName").toString()
        raceName = intent.getStringExtra("raceName").toString()
        val orgIcon = intent.getStringExtra("orgIcon")
        mDatabind.includeOrgTitle.tvIncludeOrgName.text = memberName
        mDatabind.includeOrgTitle.tvIncludeOrgRaceName.text = raceName
        ImageLoaderManager.loadCircleImageFall(this, orgIcon, mDatabind.includeOrgTitle.ivOrgIcon, R.mipmap.profilepic_str_1)

        mDatabind.includeOrgTitle.llAllVisitInfo.isVisible = true
        mDatabind.includeOrgTitle.tvVisitAnnouncement.isVisible = false

        mDatabind.recyclerViewPicProof.layoutManager = GridLayoutManager(this, 3)
        mDatabind.recyclerViewPicProof.adapter = mAdapter

        mAdapter.setOnItemClickListener { _, _, position ->
            openAdapterBigImage(position, mDatabind.recyclerViewPicProof, dataImageList)
        }

        /** 获取会员预定详情 */
        showLoading(getString(R.string.loading))
        mViewModel.getMemberReservation(mRaceId.toString())

        mDatabind.swipeRefreshLayout.init {
            mViewModel.getMemberReservation(mRaceId.toString())
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        /** 获取会员预定详情 */
        mViewModel.memberReservationResult.observe(this) {
            if (mDatabind.swipeRefreshLayout.isRefreshing) {
                mDatabind.swipeRefreshLayout.isRefreshing = false
            }
            mDatabind.data = it
            if (it.voucher.isNotEmpty()) {
                dataImageList.clear()
                dataImageList.addAll(it.voucher)
                mAdapter.setList(it.voucher)
                mAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.ivBack, mDatabind.includeOrgTitle.tvAllAppointmentInfo) {
            when (it) {
                mDatabind.includeOrgTitle.tvAllAppointmentInfo -> {
                    startActivity<ActivityAllReserveMemberInfo>("orgName" to memberName, "raceName" to raceName, "raceId" to mRaceId)
                }

                mDatabind.ivBack -> {
                    finish()
                }
            }
        }
    }
}